namespace DotNetNuke.Services.Vendors
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;

    public class BannerController
    {
        public void AddBanner(BannerInfo objBannerInfo)
        {
            DataProvider.Instance().AddBanner(objBannerInfo.BannerName, objBannerInfo.VendorId, objBannerInfo.ImageFile, objBannerInfo.URL, objBannerInfo.Impressions, objBannerInfo.CPM, objBannerInfo.StartDate, objBannerInfo.EndDate, objBannerInfo.CreatedByUser, objBannerInfo.BannerTypeId, objBannerInfo.Description, objBannerInfo.GroupName, objBannerInfo.Criteria, objBannerInfo.Width, objBannerInfo.Height);
        }

        public void DeleteBanner(int BannerId)
        {
            DataProvider.Instance().DeleteBanner(BannerId);
        }

        public string FormatBanner(int VendorId, int BannerId, int BannerTypeId, string BannerName, string ImageFile, string Description, string URL, int Width, int Height, string BannerSource, string HomeDirectory)
        {
            string strBanner = "";
            string strURL = "";
            if (BannerId != -1)
            {
                strURL = DotNetNuke.Common.Globals.ApplicationPath + "/LinkClick.aspx?BannerId=" + BannerId.ToString() + "&VendorId=" + VendorId.ToString();
            }
            else
            {
                strURL = URL;
            }
            switch (BannerTypeId)
            {
                case 6:
                    strBanner = (strBanner + "<a href=\"" + strURL + "\" class=\"NormalBold\"><u>" + BannerName + "</u></a><br>") + "<span class=\"Normal\">" + Description + "</span><br>";
                    return (strBanner + "<a href=\"" + strURL + "\" class=\"NormalRed\">" + URL + "</a>");

                case 7:
                    return (strBanner + Description);
            }
            if ((ImageFile.IndexOf("://") == -1) & !ImageFile.StartsWith("/"))
            {
                if (ImageFile.ToLower().IndexOf(".swf") == -1)
                {
                    strBanner = strBanner + "<a href=\"" + strURL + "\">";
                    switch (BannerSource)
                    {
                        case "L":
                            strBanner = strBanner + this.FormatImage(HomeDirectory + ImageFile, Width, Height, BannerName, Description);
                            break;

                        case "G":
                            strBanner = strBanner + this.FormatImage(DotNetNuke.Common.Globals.HostPath + ImageFile, Width, Height, BannerName, Description);
                            break;
                    }
                    return (strBanner + "</a>");
                }
                string refStringHelperL1 = BannerSource;
                if (refStringHelperL1 == "L")
                {
                    return (strBanner + this.FormatFlash(HomeDirectory + ImageFile, Width, Height));
                }
                if (refStringHelperL1 == "G")
                {
                    strBanner = strBanner + this.FormatFlash(DotNetNuke.Common.Globals.HostPath + ImageFile, Width, Height);
                }
                return strBanner;
            }
            if (ImageFile.ToLower().IndexOf(".swf") == -1)
            {
                return ((strBanner + "<a href=\"" + strURL + "\">") + this.FormatImage(ImageFile, Width, Height, BannerName, Description) + "</a>");
            }
            return (strBanner + this.FormatFlash(ImageFile, Width, Height));
        }

        private string FormatFlash(string File, int Width, int Height)
        {
            string Flash = "";
            Flash = ((Flash + "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0\" width=\"" + Width.ToString() + "\" height=\"" + Height.ToString() + "\">") + "<param name=movie value=\"" + File + "\">") + "<param name=quality value=high>";
            return ((Flash + "<embed src=\"" + File + "\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"" + Width.ToString() + "\" height=\"" + Height.ToString() + "\">") + "</embed>" + "</object>");
        }

        private string FormatImage(string File, int Width, int Height, string BannerName, string Description)
        {
            string Image = "";
            Image = Image + "<img src=\"" + File + "\" border=\"0\"";
            if (Description != "")
            {
                Image = Image + " alt=\"" + Description + "\"";
            }
            else
            {
                Image = Image + " alt=\"" + BannerName + "\"";
            }
            if (Width > 0)
            {
                Image = Image + " width=\"" + Width.ToString() + "\"";
            }
            if (Height > 0)
            {
                Image = Image + " height=\"" + Height.ToString() + "\"";
            }
            return (Image + "\">");
        }

        public BannerInfo GetBanner(int BannerId, int VendorId, int PortalId)
        {
            return (BannerInfo) CBO.FillObject(DataProvider.Instance().GetBanner(BannerId, VendorId, PortalId), typeof(BannerInfo));
        }

        public ArrayList GetBanners(int VendorId)
        {
            return CBO.FillCollection(DataProvider.Instance().GetBanners(VendorId), typeof(BannerInfo));
        }

        public ArrayList LoadBanners(int PortalId, int ModuleId, int BannerTypeId, string GroupName, int Banners)
        {
            ArrayList arrBanners = CBO.FillCollection(DataProvider.Instance().FindBanners(PortalId, BannerTypeId, GroupName), typeof(BannerInfo));
            ArrayList LoadBanners = new ArrayList(Banners);
            if (arrBanners.Count > 0)
            {
                int intLastBannerIndex = 0;
                object objLastBannerIndex = RuntimeHelpers.GetObjectValue(DataCache.GetCache("LastBannerIndex:" + PortalId.ToString() + ":" + ModuleId.ToString()));
                if (objLastBannerIndex != null)
                {
                    intLastBannerIndex = Convert.ToInt32(RuntimeHelpers.GetObjectValue(objLastBannerIndex));
                }
                if (Banners > arrBanners.Count)
                {
                    Banners = arrBanners.Count;
                }
                int refIntHelperL0 = Banners;
                for (int intCounter = 1; intCounter <= refIntHelperL0; intCounter++)
                {
                    DateTime datEndDate;
                    DateTime datStartDate;
                    intLastBannerIndex++;
                    if (intLastBannerIndex > (arrBanners.Count - 1))
                    {
                        intLastBannerIndex = 0;
                    }
                    BannerInfo objBanner = (BannerInfo) arrBanners[intLastBannerIndex];
                    LoadBanners.Add(objBanner);
                    if (Null.IsNull(objBanner.StartDate))
                    {
                        datStartDate = DateAndTime.Now;
                    }
                    else
                    {
                        datStartDate = objBanner.StartDate;
                    }
                    if ((Null.IsNull(objBanner.EndDate) & (objBanner.Views >= objBanner.Impressions)) & (objBanner.Impressions != 0))
                    {
                        datEndDate = DateAndTime.Now;
                    }
                    else
                    {
                        datEndDate = objBanner.EndDate;
                    }
                    DataProvider.Instance().UpdateBannerViews(objBanner.BannerId, datStartDate, datEndDate);
                }
                DataCache.SetCache("LastBannerIndex:" + PortalId.ToString() + ":" + ModuleId.ToString(), intLastBannerIndex);
            }
            return LoadBanners;
        }

        public void UpdateBanner(BannerInfo objBannerInfo)
        {
            DataProvider.Instance().UpdateBanner(objBannerInfo.BannerId, objBannerInfo.BannerName, objBannerInfo.ImageFile, objBannerInfo.URL, objBannerInfo.Impressions, objBannerInfo.CPM, objBannerInfo.StartDate, objBannerInfo.EndDate, objBannerInfo.CreatedByUser, objBannerInfo.BannerTypeId, objBannerInfo.Description, objBannerInfo.GroupName, objBannerInfo.Criteria, objBannerInfo.Width, objBannerInfo.Height);
        }

        public void UpdateBannerClickThrough(int BannerId, int VendorId)
        {
            DataProvider.Instance().UpdateBannerClickThrough(BannerId, VendorId);
        }
    }
}

